package com.atlzk.seckill.seckill.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atlzk.common.utils.PageUtils;
import com.atlzk.common.utils.Query;

import com.atlzk.seckill.seckill.dao.SeckillDao;
import com.atlzk.seckill.seckill.entity.SeckillEntity;
import com.atlzk.seckill.seckill.service.SeckillService;

import javax.annotation.PostConstruct;


@Service("seckillService")
public class SeckillServiceImpl extends ServiceImpl<SeckillDao, SeckillEntity> implements SeckillService {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SeckillEntity> page = this.page(
                new Query<SeckillEntity>().getPage(params),
                new QueryWrapper<SeckillEntity>()
        );

        return new PageUtils(page);
    }

    @PostConstruct
    public void init() {
        List <SeckillEntity> seckillEntities = baseMapper.selectList(null);
//        seckillEntities.stream().
        for (SeckillEntity seckillEntity : seckillEntities) {
            stringRedisTemplate.opsForValue().set("seckill_count" + seckillEntity.getId(), seckillEntity.getStockCount().toString());
            redisTemplate.opsForValue().set("seckill_"+ seckillEntity.getId(),seckillEntity);
        }
    }

}